package com.hangzhou.xyj.ui.activity;

import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.XYJApplication;
import com.hangzhou.xyj.event.OperationRespEvent;
import com.hangzhou.xyj.event.base.EventCode;
import com.hangzhou.xyj.manager.IntentManager;
import com.hangzhou.xyj.model.DynamicModel;
import com.hangzhou.xyj.model.DynamicPageInfoModel;
import com.hangzhou.xyj.model.type.EIntentTagType;
import com.hangzhou.xyj.net.handler.GetDynamicHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.request.GetDynamicReq;
import com.hangzhou.xyj.ui.activity.base.BaseActivity;
import com.hangzhou.xyj.ui.adapter.MyActiveAdapter;
import com.hangzhou.xyj.ui.adapter.base.BaseLoadingAdapter;
import com.hangzhou.xyj.ui.widget.CommonTitleBarView;
import com.hangzhou.xyj.ui.widget.DividerItemDecoration;
import com.hangzhou.xyj.utils.EventBusUtil;
import com.hangzhou.xyj.utils.constants.DataKey;

import org.greenrobot.eventbus.Subscribe;

import java.util.List;

public class MyActiveInfoActivity extends BaseActivity {
    private final String TAG = this.getClass().getSimpleName();
    private RecyclerView mRecyclerView;
    private MyActiveAdapter mAdapter;
    private int mPageSize = 10;

    @Override
    public void initTop() {
        enableTitle(true, "我的动态");
        enableRightNav(true, R.drawable.ic_publish);
        setOnNavRightListener(new CommonTitleBarView.OnNavRightListener() {
            @Override
            public void onNavRight() {
                Intent intent = new Intent(mContext, PublishDynamicActivity.class);
                intent.putExtra(DataKey.INTENT_TAG, EIntentTagType.TAG_MY_ACTIVE.getValue());
                IntentManager.getInstance().goActivity(mContext, intent);
            }
        });
    }


    @Override
    public void initLayouts() {
        setContentView(R.layout.activity_myactive);
    }

    @Override
    public void initViews() {
        mRecyclerView = findView(R.id.rv_my_active);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this,
                LinearLayoutManager.VERTICAL, false));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        //设置水平分割线
        mRecyclerView.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST
                , R.drawable.line_divider));
        mRecyclerView.setAdapter(mAdapter = new MyActiveAdapter(mRecyclerView, null));
        enableRefresh(true);//下拉刷新可用
        EventBusUtil.register(this);
    }

    @Override
    public void initDatas() {
        mAdapter.setIntentTag(EIntentTagType.TAG_MY_ACTIVE.getValue());
        mAdapter.setmPageSize(mPageSize);
        //请求数据
        getData(1);
    }

    @Override
    public void initListeners() {
        mAdapter.setOnLoadingListener(new BaseLoadingAdapter.OnLoadingListener() {
            @Override
            public void loading() {
                mRecyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        int currentPage = mAdapter.getCurrentPage(mPageSize);
                        Log.d(TAG, "page == " + currentPage);
                        getData(currentPage);
                    }
                }, 500);
            }
        });
    }


    public void getData(final int currentPage) {
        //请求数据
        GetDynamicReq getDynamicReq = new GetDynamicReq(Integer.valueOf(XYJApplication.getUserInfo().getUserId()),
                mPageSize, currentPage);
        HttpLoader.getInstance().sendRequest(new GetDynamicHandler(getDynamicReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                List<DynamicModel> list = ((DynamicPageInfoModel) model).getDynamicList();
                if (currentPage == 1 && (list == null || list.isEmpty())) {
                    setNoDataTip(true, "暂时没有动态，赶紧去发布动态吧~");
                } else if (currentPage == 1) {
                    mAdapter.load(list);
                } else {
                    mAdapter.update(list);
                }
            }

            @Override
            public void onFail(String msg) {
                super.onFail(msg);
                mAdapter.setLoadingNoMore();//加载失败的时候
            }
        }, 0));
    }

    @Subscribe
    public void onEventMainThread(OperationRespEvent event) {
        if (event.getCode() == EventCode.SUCCESS && TAG.equals(event.getTag())) {
            getData(1);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBusUtil.unregister(this);
    }
}
